<template>
  <div class="orders">
    <breadcrumb :breadcrumb="breadcrumb"></breadcrumb>
    <el-card class="box-card content">
      <!-- 顶部搜索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input @clear='getordersList' placeholder="请输入内容" v-model="query" class="input-with-select" clearable>
            <el-button @click="serach" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 列表 -->
      <el-table :data="ordersTableData" border style="width: 100%" stripe>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
        <el-table-column label="是否付款" width="80">
          <template v-slot="scope">
            <el-tag v-if="scope.row.pay_status==1" type="success">已付款</el-tag>
            <el-tag v-else type="warning">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column label="下单时间" width="180">
          <template v-slot="scope">{{scope.row.create_time |timestampToTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template>
            <el-tooltip
              class="item"
              :enterable="false"
              effect="light"
              content="修改订单地址"
              placement="top-start"
            >
              <el-button @click="editOrders" size="mini" type="primary" icon="el-icon-edit"></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="light"
              :enterable="false"
              content="查看物流"
              placement="top-end"
            >
              <el-button @click="checkLogistics" size="mini" type="success" icon="el-icon-location"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 修改订单地址弹框 -->
      <el-dialog
        title="修改订单地址"
        @close="resetForm('orderAddressRef')"
        :visible.sync="editOrdersDialog"
        width="50%"
      >
        <el-form
          :model="orderAddress"
          :rules="orderAddressRules"
          ref="orderAddressRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              style="width: 100%;"
              v-model="orderAddress.address1"
              :options="cityData"
              :props="{ expandTrigger: 'hover' }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="orderAddress.address2"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editOrdersDialog = false">取 消</el-button>
          <el-button type="primary" @click="editOrdersDialog = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 查看物流信息弹框 -->
      <el-dialog title="查看物流信息" :visible.sync="checkLogisticsDialog" width="50%">
        <!-- 分布条 -->
        <div class="logisticsMsg" style="height: 300px; overflow: auto;">
          <el-timeline :reverse="false">
            <el-timeline-item
              icon="el-icon-more"
              color="green"
              v-for="(activity, index) in progressInfo"
              :key="index"
              :timestamp="activity.ftime"
            >{{activity.context}}</el-timeline-item>
          </el-timeline>
        </div>
      </el-dialog>

      <!-- 分页控件 -->
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page.sync="pagenum"
        :page-sizes="[5, 10,15,20]"
        :page-size.sync="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalpage"
      ></el-pagination>
    </el-card>
  </div>
</template>
           
<script>
// 引入省市区/县数据
import cityData from "../../assets/citydata.js";
export default {
  name: "orders",
  data() {
    return {
      // 面包屑
      breadcrumb: ["订单管理", "订单列表"],
      // 订单数据列表
      ordersTableData: [],
      // 用户搜索内容
      query: "",
      // 当前页
      pagenum: 1,
      // 每页显示数据
      pagesize: 5,
      // 总页数
      totalpage: 0,
      // 修改订单地址弹框---显示与关闭
      editOrdersDialog: false,
      // 物流信息弹框--显示与关闭
      checkLogisticsDialog: false,
      // 物流信息
      progressInfo: [],
      color: "#0bbd87",
      // 订单地址信息
      orderAddress: {
        address1: [],
        address2: ""
      },
      orderAddressRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" }
        ]
      },
      // 引入省市区/县数据
      cityData: cityData
    };
  },
  methods: {
    // 初始化---获取数据
    async getordersList() {
      const { data: res } = await this.$http.get("/orders", {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      });
      if (res.meta.status != 200)
        return this.$message.error("获取订单列表数据失败!");

      // 赋值---数据
      this.ordersTableData = res.data.goods;
      // 赋值---总条数
      this.totalpage = res.data.total;
    },
    // 点击搜索
    serach() {
      this.getordersList();
    },
    // 修改订单--显示弹框
    editOrders() {
      // 显示弹框
      this.editOrdersDialog = true;
    },
    // 确认修改订单地址===暂未开发
    // 查看物流
    async checkLogistics() {
      const { data: res } = await this.$http.get("/kuaidi/1106975712662");
      this.progressInfo = res.data;
      // 显示弹框
      this.checkLogisticsDialog = true;
    },
    // 清除修改地址输入框
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 切换页
    handleCurrentChange() {
      this.getordersList();
    },
    // 切换每页显示数
    handleSizeChange() {
      this.getordersList();
    }
  },
  // 生命周期期函数===组价加载时执行
  created() {
    this.getordersList();
  },
  filters: {
    timestampToTime(time) {
      var date = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      let D = date.getDate() + " ";
      let h = date.getHours() + ":";
      let m = date.getMinutes() + ":";
      let s = date.getSeconds();
      return Y + M + D + h + m + s;
    }
  }
};
</script>
<style scoped lang='less'>
.orders {
  width: 100%;
  height: 100%;
  .content {
    margin-top: 10px;
    .el-table {
      margin: 10px 0;
    }
  }
}
</style>